package com.quanjiaolian.login.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.quanjiaolian.login.service.LoginService;
import com.quanjiaolian.login.serviceimpl.LoginServiceImpl;
import com.quanjiaolian.tools.ResponseToJson;
import com.quanjiaolian.tools.Utils;

/**
 * 登陆模块Servlet
 * @author Herson_Li
 */
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {

	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(LoginServlet.class.getName());
	
	Map<String,Object> resultMap = new HashMap<String,Object>();//返回map
	LoginService ls = new LoginServiceImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String msisdn = request.getParameter("msisdn");
		String passwd = request.getParameter("passwd");
		String type = request.getParameter("type");
		if(msisdn != null && passwd != null && type != null){
			if(ls.check(msisdn,Utils.encode(passwd.getBytes()), Integer.parseInt(type))>0){
				Map<String,Object> map = ls.queryUserInfoById(msisdn, Integer.parseInt(type));
				resultMap.put("result","OK");
				resultMap.put("userinfo",map);
				ResponseToJson.responseOutWithJson(response,resultMap);
			}else{
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("result","Failed");
				ResponseToJson.responseOutWithJson(response,map);
			}
		}else{
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("result","Error");
			ResponseToJson.responseOutWithJson(response,map);
		}
		
	}

	

}
